package com.gouying.mall.rabbit.mq;

import com.alibaba.fastjson.JSON;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.Map;

/**
 * 这块有点绕
 * 1：先设置两个通道 一个死信通道 然后一一对应  然后两个通道相连接（bingding）
 * 2死信队列在过期后 指向一个routekey（不能相同）
 * 3 所以deadqueue-deadexchange route-key是dead
 *       deadqueue 过期之后发向normalexchange 就是fuhuo的routekey
 *       normalqueue-normalexchange - fuhuokey
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class DeadRabbitMq {

    static int i =  3;
    private static final String exchange_dead = "exchange.dead"+i;
    private static final String exchange_normal = "exchange.normal"+i;

    private static final String queue_dead = "queue_dead"+i;

    private static final String queue_normal = "queue_normal"+i;



    @Autowired
    RabbitAdmin rabbitAdmin;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Test
    public void createDead() {
        // 普通队列的声明
        Map<String, Object> arguments = new HashMap<>();
        // 过期时间
        arguments.put("x-message-ttl", 1000 * 10);
        // 正常队列设置死信交换机
        arguments.put("x-dead-letter-exchange", exchange_dead);
        // 设置死信 路由 就是过期之后发出的路由key
        arguments.put("x-dead-letter-routing-key", "fuhuo");
        rabbitAdmin.declareExchange(new DirectExchange(exchange_dead, true, false, null));
        rabbitAdmin.declareQueue(new Queue(queue_dead, true, false, false, arguments));
        rabbitAdmin.declareBinding(new Binding(queue_dead, Binding.DestinationType.QUEUE,exchange_dead,"dead",null));


    }

    @Test
    public void createNormal() {
        rabbitAdmin.declareQueue(new Queue(queue_normal, true, false, false, null));
        rabbitAdmin.declareExchange(new DirectExchange(exchange_normal, true, false, null));
        rabbitAdmin.declareBinding(new Binding(queue_normal, Binding.DestinationType.QUEUE,exchange_normal,"fuhuo",null));
    }

    @Test
    public void binding() {
        createDead();
        createNormal();
        // 目的地就是交换机发送到哪的地方 这里发送到队列
        Binding.DestinationType destinationType = Binding.DestinationType.EXCHANGE; //【目的地类型】
        rabbitAdmin.declareBinding(new Binding(exchange_normal, destinationType, exchange_dead, "fuhuo",null));
    }


    @Test
    public void send() {
        rabbitTemplate.convertAndSend(exchange_dead, "dead", "test-routing-value");
    }

    @Test
    public void getMsg() {
        Message receive = rabbitTemplate.receive("test-queue");
        System.out.println(JSON.toJSONString(receive));
    }

}
